﻿#include "conview.h"
#include "ui_conview.h"
#include "urparser.h"
#include <QDebug>
ConView* ConView::_controlplat = nullptr;

ConView::ConView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ConView)
{
    ui->setupUi(this);
}

ConView::~ConView()
{
    delete ui;
}

ConView *ConView::Instance()
{
    if(ConView::_controlplat == nullptr)
    {
        _controlplat = new ConView();
    }
    return ConView::_controlplat;
}

QString ConView::getIp()
{
    return _ip;
}

unsigned int ConView::getPort()
{
    return _port;
}

void ConView::slot_connecttcp()
{
    ui->label_light->setStyleSheet("background-color: rgb(0, 255, 0);");
    emit sig_connected();
}

void ConView::slot_disconnected()
{
    ui->label_light->setStyleSheet("background-color: rgb(255,0,0);");
    emit sig_disconnected();
}

void ConView::on_pushButton_connect_clicked()
{
    static bool connected=false;
    if(!connected)
    {
        QString ip=ui->lineEdit_ipinput->text();
        unsigned int port = ui->lineEdit_portinput->text().toUInt();
        _ip=ip;
        _port=port;
        //emit sig_connectstart(ip,port);
        URParser::Instance()->connect(_ip,_port);
    }
    else
    {
        URParser::Instance()->disconnect();
    }

    if(URParser::Instance()->isconnected())
    {
        qDebug()<<"connect success!";
        ui->label_info->setText("Connected");
        slot_connecttcp();
        connected=true;
    }
    else
    {
        qDebug()<<"connect failed";
        ui->label_info->setText("DisConnected");
        slot_disconnected();
        connected=false;
    }
}

